/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.conditions 
{
	import net.babeuf.form.fields.AbstractField;
	
	/**
	 * MatchValueCondition condition is used to check if a field content matches a defined value. 
	 * 
	 * @example
	 * <listing>
	 * var answer : NumericField = new NumericField( myField ) ; 
	 *
	 * 
	 * var condition : MatchValueCondition = new MatchValueCondition( answer, 42 ) ; 
	 * 
	 * trace(condition.match()) ; // outputs true if the answer is 42
	 * 
	 * </listing> 
	 * 
	 * @author damien pegoraro 
	 *  
	 * @langversion 3.0
	 * @playerversion Flash 10
	 */
	public class MatchValueCondition extends AbstractCondition implements ICondition
	{
		/**
		 * The <code>AbstractField</code> containing the user value
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		protected var _field : AbstractField  ;	
		
		/**
		 * The blob value to match
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		protected var _value : * ; 
		
		/**
		 * Is value checking case sensitive
		 *  
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		protected var _caseSensitive : Boolean ; 
	
		
		/**
		 * Creates a new instance
		 * 
		 * @param	field  the <code>AbstractField</code> to check
		 * @param	value the blob value to match
		 * @param   caseSensitive  is value checking case sensitive
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		public function MatchValueCondition( field : AbstractField, value : *, caseSensitive : Boolean = true ) 
		{
			_caseSensitive = caseSensitive ;
			_field = field ; 
			_value = value ; 
		}
		
		/**
		 * @inheritDoc
		 */
		override public function match():Boolean 
		{
			if ( !_caseSensitive ) 
			{
				
				if ( String( _field.getValue()).toUpperCase() !=  String(_value).toUpperCase()  ) { _updateSensors(false) ; return false ; }
			}
			else 
			{
				
				if (  _field.getValue() != _value  ) { _updateSensors(false) ; return false ; }
			}
			return true ; 
		}
		
		/**
		 * @inheritDoc
		 */
		override public function dispose():void 
		{
			super.dispose();
			_field = null ; 
			_value = null ; 
		}
		
		
		
		
	}

}